<?php


namespace App\Http\Controllers;


use App\Models\PartitionAdjustmentAmount;
use App\Services\Java\ApiService;
use App\Services\Login;
use D1M\Common\Exceptions\BusinessException;
use D1M\Common\Helpers\Result;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FoodOrderController
{

    /**
     * 调整金额（优惠券、手动调整）
     * @param Request $request
     * @param Login $service
     * @param ApiService $api
     * @return array
     */

    public function adjustment(Request $request,Login $service,ApiService $api)
    {
        $params = $request->only(['order_no','init_amount','after_amount',
            'reduce_id','reduce_amount','adjustment','reduce_description',
            'adjustment_type','order_type']);

        //if($params['init_amount']<=0 || $params['after_amount']<0)
        if($params['after_amount']<0)
        {
            return Result::fail('金额不合规');
        }


        //调WH接口 确认是否符合登录条件



            //修改餐饮单调Java/food/api/order/update  修改零售单/product/api/product/update
            $paramsData = [
                'orderNo'=>$params['order_no'],
                'reduceId'=>$params['reduce_id']??null,
                'reduceAmount'=>$params['reduce_amount']??null,
                'reduceName'=>$params['reduce_description']??'',
                //'adjustAmount'=>$params['after_amount'],
                'adjustMode'=>$params['adjustment']??null,
                //'adjustUser'=>$name,
            ];
            if(!isset($params['reduce_id'])){
                $user = $service->me($request);
                //$manageNo = $user->manage_no;
                $name = $user->real_name??'';
                $paramsData['adjustUser']= $name;
                $paramsData['adjustAmount']= $params['after_amount'];
            }

            if($params['order_type']){
                //零售单
                $uri = '/product/api/product/update';
            }else{
                //餐饮单
                $paramsData['sumPrice'] = $params['init_amount'];
                $uri = '/food/api/order/update';
            }

            $data = $api->requestJava($uri,$paramsData,'post');
            if(!$data) {
                return Result::fail($uri.'调整失败');
            }

            return Result::success(null,'调整成功');

    }



    public function clearAdjustment(Request $request,ApiService $api)
    {
        $params = $request->only(['order_no','init_amount', 'order_type']);


        $paramsData = ['orderNo'=>$params['order_no']];
        if($params['order_type']){
            //零售单
            //return Result::fail('参数异常，零售单不可用券且不可取消用券');
            $uri = '/product/api/product/clear-reduce-info';
        }else{
            //餐饮单
            $uri = '/food/api/order/reduce-clear';
        }

        $data = $api->requestJava($uri,$paramsData,'post');
        if(!$data)
        {
            return Result::fail($uri.'取消用券失败');
        }

        return Result::success(null,'取消用券成功');
    }


    /**
     * 调整金额（优惠券、手动调整）
     * @param Request $request
     * @param Login $service
     * @param ApiService $api
     * @return array
     */

    public function adjustment_abondon(Request $request,Login $service,ApiService $api)
    {
        $params = $request->only(['order_no','init_amount','after_amount',
            'reduce_id','reduce_amount','adjustment','reduce_description',
            'adjustment_type','order_type']);

        //if($params['init_amount']<=0 || $params['after_amount']<0)
        if($params['after_amount']<0)
        {
            return Result::fail('金额不合规');
        }


        //调WH接口 确认是否符合登录条件
        $user = $service->me($request);
        $manageNo = $user->manage_no;
        $name = $user->real_name??'';

        //存储调整记录
        $params['manage_no'] = $manageNo;
        $params['login_name'] = $name;
        if(!empty($params['adjustment'])) {
            $params['adjustment'] =json_encode($params['adjustment']);
        }else{
            unset($params['adjustment']);
        }
        try{
            DB::beginTransaction();
            //删除原来的
            //if($params['adjustment_type']===0){
                PartitionAdjustmentAmount::where(
                    [
                        'order_no'=>$params['order_no'],
                        'adjustment_type'=>$params['adjustment_type'],
                        'order_type'=>$params['order_type'],
                    ])
                    ->update(['deleted_at'=>date("Y-m-d H:i:s")]);
            //}

            PartitionAdjustmentAmount::insert($params);
            //修改餐饮单调Java/food/api/order/update  修改零售单/product/api/product/update
            $paramsData = [
                'orderNo'=>$params['order_no'],
                'reduceId'=>$params['reduce_id']??null,
                'reduceAmount'=>$params['reduce_amount']??0,
                'adjustAmount'=>$params['after_amount'],
            ];
            
            //餐饮单
            $paramsData['sumPrice'] = $params['init_amount'];
            $uri = '/food/api/order/update';
            if($params['order_type']){
                //零售单
                $paramsData['reduceName'] = $params['reduce_description']??'';
                $uri = '/product/api/product/update';
            }
            
            $data = $api->requestJava($uri,$paramsData,'post');
            if(!$data) {
                DB::rollBack();
                return Result::fail($uri.'调整失败');
            }

            DB::commit();
            return Result::success(null,'调整成功');
        }catch (\Exception $e)
        {
            DB::rollBack();
            return Result::fail($e->getMessage());
        }


    }
    
    
    /**
     * 清空优惠券的选择
     */
    public function clearAdjustment_abondon(Request $request,ApiService $api)
    {
        $params = $request->only(['order_no','init_amount', 'order_type']);

        try{
            DB::beginTransaction();
            PartitionAdjustmentAmount::where(
                [
                    'order_no'=>$params['order_no'],
    //                'adjustment_type'=>$params['adjustment_type'],
                    'order_type'=>$params['order_type'],
                ])
                ->update(['deleted_at'=>date("Y-m-d H:i:s")]);

            $paramsData = [
                'orderNo'=>$params['order_no'],
                'reduceId'=>null,
                'reduceAmount'=>0,
                'adjustAmount'=>$params['init_amount'],
            ];

            //餐饮单
            $paramsData['sumPrice'] = $params['init_amount'];
            $uri = '/food/api/order/update';
            if($params['order_type']){
                //零售单
                $paramsData['reduceName'] = '';
                $uri = '/product/api/product/update';
            }

            $data = $api->requestJava($uri,$paramsData,'post');
            if(!$data)
            {
                DB::rollBack();
                return Result::fail($uri.'取消用券失败');
            }
            DB::commit();
            return Result::success(null,'取消用券成功');
        }catch (\Exception $e)
        {
            DB::rollBack();
            return Result::fail($e->getMessage());
        }

    }


}